<?php

class user_actions_traceActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
  	$this->form = new LanguageForm();
    
    $this->traces = Doctrine_Core::getTable('UserActionsTrace')->filter(date('m-d-Y'), date('m-d-Y'), null);
    
    $this->users = Doctrine_Core::getTable('User')->getAll();
    
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->language = Doctrine_Core::getTable('Language')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->language);
    
    $this->form = new LanguageForm($this->language);
    
    return $this->renderPartial('language',array('form'=>$this->form));    
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new LanguageForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $form = new LanguageForm();

    $this->processForm($request, $form);
    
    $this->languages = Doctrine_Core::getTable('Language')->getAll();
    
    $language = new Language();
  	
  	$this->form = new LanguageForm($language);
  	
    $action_str = "Language created ID: " . $form->getObject()->getId();
  	DirectoryUtils::registerTrace($action_str, $this->getUser()->getAttribute("user_id"));     	
    
    return $this->renderPartial('language_container',array('languages' => $this->languages,  'form' => $this->form));    
    
    
  }

  public function executeEdit(sfWebRequest $request)
  {
  
    $this->forward404Unless($language = Doctrine_Core::getTable('Language')->find(array($request->getParameter('id'))), sprintf('Object language does not exist (%s).', $request->getParameter('id')));
    $this->form = new LanguageForm($language);
  }

  public function executeUpdate(sfWebRequest $request)
  {
  	
  	
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($language = Doctrine_Core::getTable('Language')->find(array($request->getParameter('id'))), sprintf('Object master_directory does not exist (%s).', $request->getParameter('id')));
    $form = new LanguageForm($language);
    

    $this->processForm($request, $form);
    
    
    $this->languages = Doctrine_Core::getTable('Language')->getAll();
    
    $language = new Language();
  	
  	$this->form = new LanguageForm($language);
  	
    $action_str = "Language updated ID: " . $form->getObject()->getId();
  	DirectoryUtils::registerTrace($action_str, $this->getUser()->getAttribute("user_id"));    	
    
    return $this->renderPartial('language_container',array('languages'=>$this->languages, 'form' => $this->form));  	
  	
  }
  
  public function executeLoadLanguageData(sfWebRequest $request){
  	
  	
  	$language = new Language();
  	
  	$this->form = new LanguageForm($language);
  	
    $this->languages = Doctrine_Core::getTable('Language')->getAll();
    
    return $this->renderPartial('table',array('languages'=>$this->languages));
  }
  

  public function executeDelete(sfWebRequest $request)
  {
  	
  	
  	$this->forward404Unless($language = Doctrine_Core::getTable('Language')->find(array($request->getParameter('id'))), sprintf('Object master_directory does not exist (%s).', $request->getParameter('id')));
    $language->delete();
    
    $this->getUser()->setFlash('save', 'deleted', false);
    
    $this->languages = Doctrine_Core::getTable('Language')->getAll();
    
    $this->form = new LanguageForm();
    
    $action_str = "Language deleted ID: " . $request->getParameter('id');
  	DirectoryUtils::registerTrace($action_str, $this->getUser()->getAttribute("user_id"));      
    
    return $this->renderPartial('language_container',array('languages'=>$this->languages,  'form' => $this->form));
  	
  }
  
  public function executeFilter(sfWebRequest $request){
  	$ini_date = $request->getParameter('initial_date');
  	$end_date = $request->getParameter('end_date');
  	$user = $request->getParameter('sl_users');
  	
  	$traces = Doctrine_Core::getTable('UserActionsTrace')->filter($ini_date, $end_date, $user);
  	
  	return $this->renderPartial('table', array('traces' => $traces));
  	
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
  	
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    
    if ($form->isValid())
    {
    	
      $language = $form->save();
      $this->getUser()->setFlash('save', 'saved', false);

    }else {
    }

  }
 
}
